package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.xspace.android.xafcommon.been.DraftBean;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.util.LogUtil;


public class DraftMsgDB extends BaseSql {
	private final String msgId = "msgId";
	private final String toUserId = "toUserId";
	private final String ownerId = "ownerId";
	private final String inserTime = "insertTime";
	private final String content = "content";
	/**草稿类型 0个人 1群组 2圈子 3多人会话**/
	private String msgType = "msgtype";
	private String TableName = "DraftMsg";
	private MyDBHelper helpDb;
	private SQLiteDatabase sqldb;
	private String tag="DraftMsgDB";
	@Override
	public void create(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TableName
				+ " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," + inserTime + " integer64,"
				+ msgId + " text," + toUserId + " text," + ownerId + " text,"
				+ msgType + " integer,"
				+ content + " text);");
	}
	public DraftMsgDB(MyDBHelper db) {
		super(db);
		helpDb=db;
	}
	public DraftMsgDB() {
	}
	public void dropTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TableName);
	}
	@Override
	public void upgrade(SQLiteDatabase arg0) {

	}
	private void openDB(){
		helpDb.open();
		sqldb = helpDb.mDatabase;

	}
    public  void insertOrUpdate(DraftBean draft) {
    	if(draft==null)  return;
		openDB();
		ContentValues cv=new ContentValues();
		String[] columns={content,ownerId,toUserId,msgType};
		String selection=toUserId+"=? and "+ownerId+" =? and "+msgType+"=?";
		String[] selectionArgs=new String[]{draft.getToUserId(),draft.getOwnerId(),draft.getMsgType()+""};
		Cursor cur=sqldb.query(TableName, columns,selection ,selectionArgs , null, null, null);
		if(cur!=null&&cur.getCount()>0){
			cv.put(content, draft.getContent());
			cv.put(inserTime, draft.getInserTime());
			sqldb.update(TableName, cv, selection, selectionArgs);
			LogUtil.v(tag, "insertOrUpdate update>>>" + draft.getContent());
		}
		else{
			cv.put(toUserId, draft.getToUserId());
			cv.put(ownerId, draft.getOwnerId());
			cv.put(msgType, draft.getMsgType());
			cv.put(content, draft.getContent());
			cv.put(inserTime, draft.getInserTime());
			long result=sqldb.insert(TableName, null, cv);
			LogUtil.v(tag, "insertOrUpdate insert>>>"+draft.getContent()+" result:"+result);
		}
		cur.close();

	}
    public String queryDraftContent(DraftBean draft) {
		String draftcontent=null;
		openDB();
		String[] columns={content,ownerId,toUserId,msgType};
		String selection=toUserId+"=? and "+ownerId+" =? and "+msgType+"=?";
		String[] selectionArgs=new String[]{draft.getToUserId(),draft.getOwnerId(),draft.getMsgType()+""};
		Cursor cur=sqldb.query(TableName, columns,selection ,selectionArgs , null, null, null);
		if(cur!=null&&cur.getCount()>0){
			if(cur.moveToFirst()){
				draftcontent=cur.getString(0);
			}
		}
		cur.close();
		LogUtil.v(tag, "queryDraftContent content:"+draftcontent+" "+" ownerId:"+draft.getOwnerId()+" toUserId:"+draft.getToUserId());
		return draftcontent;
	}
    /**
     * 
     * @param context
     * @param mMsgType 0个人  1圈子 2群组 3多人会话
     * @return
     */
    public String queryDraftContentByMsgType(Context context,int mMsgType) {
		String draftcontent=null;
		String userid=null;
		String mownerId= "";
		openDB();
		String[] columns={content,ownerId,toUserId,msgType};
		String selection=ownerId+" =? and "+msgType+"=?";
		String[] selectionArgs=new String[]{mownerId,mMsgType+""};
		Cursor cur=sqldb.query(TableName, columns,selection ,selectionArgs , null, null, inserTime +" desc");
		if(cur!=null&&cur.getCount()>0){
			if(cur.moveToFirst()){
				draftcontent=cur.getString(0);
				userid=cur.getString(2);
				if(TextUtils.isEmpty(draftcontent)){
				while (cur.moveToNext()) {
					draftcontent=cur.getString(0);
					userid=cur.getString(2);
					if(!TextUtils.isEmpty(draftcontent)){
						break;
					}
				}
				}
			}
		}
		cur.close();
		LogUtil.v(tag, "queryDraftContentByMsgType content:"+draftcontent+" "+" mMsgType:"+mMsgType+" userid:"+userid);
		return draftcontent;
	}
}
